import Util from "../../utils/util"

// pages/index/mch.js
const App = getApp()

Component({
  options: {
    addGlobalClass: true,
  },
  properties: {

  },
  data: {
    init: false,
    summ: {},
    mgr: false,
    createWxAppDialog: false,
    isSubmitCreateWxApp: false
  },

  lifetimes: {
    attached() {
      this.setData({
        mgr: App.Global.User.merchant.mgr
      })
      this.execLoad(false)
    },
    detached() {},
  },
  pageLifetimes: {
    show() {
      if (this.data.init) {
        this.execLoad(false)
      }
    }
  },
  methods: {
    execLoad(pulldown) {
      // console.log(pulldown)
      App.doPost({
        uri: '/app/merchant/stat/op/summary',
        body: {},
        fnSucc: resp => {
          this.setData({
            init: true,
            summ: resp.data
          })
          if (pulldown) {
            this.triggerEvent('stopPullDown', {})

          }
        }
      })
      const submitCreateWxApp = wx.getStorageSync('submitCreateWxApp')
      if(submitCreateWxApp){
        this.setData({
          isSubmitCreateWxApp: true
        })
      }
    },
    doPulldown() {
      this.execLoad(true)
    },
    doReachBottom() {

    },
    toPage(e) {
      let mgr = e.currentTarget.dataset.mgr == 'true'
      if (mgr && !this.data.mgr) {
        Util.Toast.fail('您的账户无权限操作')
        return
      }
      wx.navigateTo({
        url: e.currentTarget.dataset.uri
      })
    },
    toGpsPage(){
      Util.Toast.fail('功能暂未开放，敬请期待')
    },
    openCreateWxApp(){
      this.setData({
        createWxAppDialog: true
      })
    },
    closeCreateWxApp(){
      this.setData({
        createWxAppDialog: false
      })
    },
    submitCreateWxApp(){
      const userStr = wx.getStorageSync('App.User')
      const user = JSON.parse(userStr)
      console.log('user', user)
      Util.Dialog.confirm(`提交申请后，我们将尽快联系您，请确保手机畅通`, () => {
        // wx.request({
        //   url: App.Global.API + '/app/phone_num/save',
        //   method: 'POST',
        //   data: {
        //     phone_num: user.merchant.mob,
        //   },
        //   success: (res) => {
        //     console.log('res', res)
        //   },
        //   fail: (err) => {
        //     console.log('err', err)
        //   }
        // })
        const params = {
          mobile: user.merchant.mob,
        }
        console.log('params', params)
        App.doPost({
          uri: '/app/phone_num/save',
          body: params,
          fnSucc: () => {
            Util.Toast.succ('提交成功')
            // 已提交过申请
            wx.setStorageSync('submitCreateWxApp', "1")
            this.setData({
              createWxAppDialog: false,
              isSubmitCreateWxApp: true
            })
          },
          fnFail: resp => {
            Util.Notify.error(`[${resp.stat.err}] ${resp.stat.msg}`)
          },
        })

      })
    }
  }
})